﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SQLite;
namespace ShopMS.Data
{
    public enum DataAccessType
    {
        Sqlite = 0
    }
    public class DataAccess : IDataAccess
    {
        private IDbConnection conn;
        private DataAccessType accessType;
        private string connectionString;

        public IDbConnection getConnection()
        {
            string _accessType = System.Configuration.ConfigurationManager.AppSettings["accessType"];
            DataAccessType accType = (DataAccessType)Enum.ToObject(typeof(DataAccessType), int.Parse(_accessType));
            return getConnection(accType);
        }

        public IDbConnection getConnection(DataAccessType _accessType)
        {
            DataAccessType __accessType = _accessType;
            string _connectionString = System.Configuration.ConfigurationManager.AppSettings["connString"];
            return getConnection(__accessType,_connectionString);
        }

        public IDbConnection getConnection(DataAccessType _accessType, string _connString)
        {
            this.accessType = _accessType;
            this.connectionString = _connString;
            switch (accessType)
            {
                case DataAccessType.Sqlite:
                    getSqliteConnection();
                    break;
            }
            return conn;
        }

        private void getSqliteConnection()
        {
            conn = new SQLiteConnection(this.connectionString);
        }

        public DataTable getDataTable(string sql)
        {
            IDbCommand cmd = getCommand(sql);
            conn.Open();
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            conn.Close();
            return dt;
        }

        public DataSet getDataSet(string sql)
        {
            IDbCommand cmd = getCommand(sql);
            IDataAdapter adpt = getDataAdpter(cmd);
            DataSet dst = new DataSet();
            adpt.Fill(dst);
            return dst;
        }

        public int executeNonQuery(string sql) {
            IDbCommand cmd = getCommand(sql);
            conn.Open();
            int result = cmd.ExecuteNonQuery();
            conn.Close();
            return result;
        }

        public object executeScalar(string sql) 
        {
            IDbCommand cmd = getCommand(sql);
            conn.Open();
            object result = cmd.ExecuteScalar();
            conn.Close();
            return result;
        }

        private IDbCommand getCommand(string commandText)
        {
            return getCommand(commandText, CommandType.Text);
        }

        private IDbCommand getCommand(string commandText,CommandType commandType)
        {
            if (conn == null)
                conn = getConnection();
            return getSqliteCommand(commandText, commandType);
        }
        private IDataAdapter getDataAdpter(IDbCommand cmd)
        {
            return getSqliteDataAdapter(cmd);
        }

        #region sqlite

        private IDbCommand getSqliteCommand(string commandText, CommandType commandType)
        {
            SQLiteCommand cmd = new SQLiteCommand((SQLiteConnection)conn);
            cmd.CommandText = commandText;
            cmd.CommandType = commandType;
            return cmd;
        }
        private static IDataAdapter getSqliteDataAdapter(IDbCommand cmd)
        {
            SQLiteCommand command = (SQLiteCommand)cmd;
            SQLiteDataAdapter adpt = new SQLiteDataAdapter(command);
            return adpt;
        }

        #endregion

    }
}
